package com.sunlg.smarthome.ui.warning;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Window;

import com.androidkun.xtablayout.XTabLayout;
import com.sunlg.smarthome.R;

import java.util.ArrayList;
import java.util.List;

public class WarningActivity extends AppCompatActivity {

    private ViewPager mViewPager;
    private FragmentPagerAdapter mAdapter;
    private List<Fragment> mFragments;
    private Toolbar toolbar;

    private SecurityWarningFragment securityWarningFragment;
    private FireWarningFragment fireWarningFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_warning);
        init();
        initView();
    }

    private void init(){
        securityWarningFragment = new SecurityWarningFragment();
        fireWarningFragment = new FireWarningFragment();

    }

    private void initView(){
        toolbar = (Toolbar)findViewById(R.id.myToolBar);
        setSupportActionBar(toolbar);

        mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
        mFragments = new ArrayList<>();
        mFragments.add(securityWarningFragment);
        mFragments.add(fireWarningFragment);

        List<String> tiles=new ArrayList<>();
        tiles.add("安防警报");
        tiles.add("防火警报");

        mAdapter=new WarningViewPagerAdapter(getSupportFragmentManager(),mFragments,tiles);
        mViewPager.setAdapter(mAdapter);
        XTabLayout mTabLayout = (XTabLayout) findViewById(R.id.tabLayout);
        mTabLayout.addTab(mTabLayout.newTab().setText("安防警报"));//给TabLayout添加Tab
        mTabLayout.addTab(mTabLayout.newTab().setText("防火警报"));

        //给TabLayout设置关联ViewPager，如果设置了ViewPager，那么ViewPagerAdapter中的getPageTitle()方法返回的就是Tab上的标题

        mTabLayout.setupWithViewPager(mViewPager);//给TabLayout设置关联ViewPager，如果设置了ViewPager，那么

        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("警报信息");
        actionBar.setDisplayHomeAsUpEnabled(true);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
